package com.gsafety.wellsfort.controller.knowledge;

import com.gsafety.wellsfort.common.util.ApiResponse;
import com.gsafety.wellsfort.common.util.ExcelUtil;
import com.gsafety.wellsfort.domain.dto.ids.QuestionIdDTO;
import com.gsafety.wellsfort.domain.dto.question.QuestionDTO;
import com.gsafety.wellsfort.domain.dto.question.QuestionQueryDTO;
import com.gsafety.wellsfort.domain.model.knowledge.Question;
import com.gsafety.wellsfort.domain.model.organization.School;
import com.gsafety.wellsfort.domain.vo.QuestionExcelVO;
import com.gsafety.wellsfort.domain.vo.QuestionVO;
import com.gsafety.wellsfort.service.knowledge.QuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import java.util.UUID;

@Api(value = "题库管理",description = "题库管理接口")
@RestController
@Slf4j
@RequestMapping("/api/question")
public class QuestionController {
    @Autowired
    private QuestionService questionService;
    @ApiOperation(value="保存试题")
    @PostMapping("/save")
    public ApiResponse<QuestionDTO> saveQuestion(@RequestBody QuestionDTO qDto){
        return questionService.saveQuestion(qDto);
    }

    @ApiOperation(value="删除试题")
    @PostMapping("/delete")
    public ApiResponse deleteQuestion(@RequestBody QuestionIdDTO idDto){
        return questionService.deleteQuestion(idDto.getQuestionId());
    }

    @ApiOperation(value="编辑试题")
    @PostMapping("/get")
    public ApiResponse<QuestionVO> getQuestion(@RequestBody QuestionIdDTO idDto){
        return questionService.getQuestion(idDto.getQuestionId());
    }

    @ApiOperation(value="查询试题")
    @PostMapping("/list")
    public ApiResponse<List<QuestionVO>> findByPageParams(@RequestBody QuestionQueryDTO voParams){
        return questionService.findByPageParams(voParams);
    }

    @ApiOperation(value = "批量添加试题")
    @RequestMapping(value = "/batchAdd", method = RequestMethod.POST)
    public ApiResponse batchAdd(@RequestParam("fileName") MultipartFile file) throws Exception {
        // 获取文件名
        String fileName = file.getOriginalFilename();
        // 获取文件后缀
        String prefix = fileName.substring(fileName.lastIndexOf("."));
        // 用uuid作为文件名，防止生成的临时文件重复
        final File excelFile = File.createTempFile(UUID.randomUUID().toString(), prefix);
        // MultipartFile to File
        file.transferTo(excelFile);
        List<QuestionExcelVO> list = ExcelUtil.getDtoList(excelFile, QuestionExcelVO.class);
        questionService.batchAddQuestion(list);
        // 程序结束时，删除临时文件
        deleteFile(excelFile);
        return ApiResponse.success();
    }
    /**
     * 删除
     *
     * @param files
     */
    private void deleteFile(File... files) {
        for (File file : files) {
            if (file.exists()) {
                file.delete();
            }
        }
    }
}
